<?php
	defined('_JEXEC') or die('Restricted access');
	require_once(CUTEENGINE_BASE_FIELD_PATH."base_field_class.php");
    
	//
    // t_field_redirect
    //
	class t_field_redirect extends t_base_field
	{
        
        // property declaration
		// protected

        

        // property declaration
		// public
		// G
		const language_position_last = 1;
		
		// C
		const redirect_type_from_address = 1;
		const redirect_type_from_db = 2;
		const redirect_type_from_custom_file = 3;
		const redirect_type_from_js_redirect = 4;
		const redirect_type_from_js_db_redirect = 5;
		const redirect_type_last = 6;
		// P
		const proper_properties_position_redirect_type = 1;
		const proper_properties_position_param_1 = 2;  //destination, query, filename,js timer,query
		const proper_properties_position_param_2 = 3;  //not used, query_parameters_names, not used,not used,query_parameters_names
		const proper_properties_position_param_3 = 4;  //not used, not used , not used, not used, js timer
        const proper_properties_position_last = 5;

        // method declaration
    	// protected
		// C
		// G
		protected function get_action_name($a_prefix)
        {
            return $a_prefix . "_" . $this->name;
        } 
		protected function get_field_event_script($a_event_type,$a_argouments)
        {			
			//echo "a_event_type:$a_event_type<BR>";
			$local_return = '';
			return $local_return;
		}
static function get_help_proper_property($a_index)
		{
			$local_return = 'NO HELP AVAILABLE';
			/*switch ($a_index) 
            {
                case self::proper_properties_position_:
					$local_return = '';
				break;                
			}*/
			return $local_return;
		}
        static function get_default_proper_property($a_index)
		{
			$local_return = '';
			/*switch ($a_index) 
            {
                case self::proper_properties_position_:
					$local_return = 'TRUE';
				break;                
			}*/
			return $local_return;
		}
		protected function get_proper_properties_position_last()     
		{
            return self::proper_properties_position_last;
        }
		
	
		
        // method declaration
    	// public
        public function __construct($a_parent_part,$a_common_properties_str, $a_proper_properties_str)
		{
            parent::__construct($a_parent_part,$a_common_properties_str, $a_proper_properties_str);
            global $g_cuteengine;
			global $g_ambient_namespace;
			if ($g_ambient_namespace->user_agent->is_bot_agent)
				return ;
			switch ($this->proper_properties_array[self::proper_properties_position_redirect_type]) 
			{
				
				case self::redirect_type_from_address:
					
				break;
				case self::redirect_type_from_db:
					// initialization
					$local_params = '["'.str_replace(t_strings_utility_namespace::separator_level_2, '","', substr($this->proper_properties_array[self::proper_properties_position_param_2],1,-1)).'"]';
					//echo "part name: ".$this->parent_part->get_part_name()."<br>";
					$this->parent_part->add_action_for_field_uses_as_included('["none","t_action_execute_query","'.$this->get_action_name('from_query').'",["'.$this->name.'"],"'.
						$this->proper_properties_array[self::proper_properties_position_param_1].'",'.$local_params.',"FALSE","FALSE"]');
					
					
				break;
				case self::redirect_type_from_custom_file:
					
				break;
				case self::redirect_type_from_js_redirect:
					/*$local_params = '["'.str_replace(t_strings_utility_namespace::separator_level_2, '","', substr($this->proper_properties_array[self::proper_properties_position_param_2],1,-1)).'"]';
					$this->parent_part->add_action_for_field_uses_as_included('["none","t_action_execute_query","'.$this->get_action_name('from_query').'",["'.$this->name.'"],"'.
						$this->proper_properties_array[self::proper_properties_position_param_1].'",'.$local_params.',"FALSE","FALSE"]');			*/		
				break;		
				case self::redirect_type_from_js_db_redirect:
					// initialization
					$local_params = '["'.str_replace(t_strings_utility_namespace::separator_level_2, '","', substr($this->proper_properties_array[self::proper_properties_position_param_2],1,-1)).'"]';
					//echo "part name: ".$this->parent_part->get_part_name()."<br>";
					$this->parent_part->add_action_for_field_uses_as_included('["none","t_action_execute_query","'.$this->get_action_name('from_query').'",["'.$this->name.'"],"'.
						$this->proper_properties_array[self::proper_properties_position_param_1].'",'.$local_params.',"FALSE","FALSE"]');
					
				break;					
				
			}	
			
		}
		// A
		public function apply_result_script($a_query_parameter,$a_current_row_no,$a_rows_count,$a_arguments)
        {
			
			$local_action = $a_arguments[0];
            $local_action_name = $local_action->get_action_name();
			//echo "local_action_name: $local_action_name<br>";
            $local_result = $a_arguments[1];
           // print_r($local_result);
		    global $g_ambient_namespace;
			if ($g_ambient_namespace->user_agent->is_bot_agent)
				return ;
 			switch ($this->proper_properties_array[self::proper_properties_position_redirect_type]) 
			{

				case self::redirect_type_from_address:
					
				break;
				case self::redirect_type_from_db:
					
					$this->do_redirect($a_query_parameter->sql_values[0]);
				
				break;   
				case self::redirect_type_from_custom_file:
					
				break;	
				case self::redirect_type_from_js_redirect:
					/*$local_result->add_function_result(
						"","","",
						"window.location.replace('".str_replace("'", "\'",
						$this->proper_properties_array[self::proper_properties_position_param_1] ."');"));*/
				break;		
				case self::redirect_type_from_js_db_redirect:
					//echo $a_query_parameter->retrive_sql_query_with_value_parameter($a_query_parameter->sql_str);
					echo "REINDIRIZZAMENTO ALLA PAGINA ".$a_query_parameter->sql_values[0]." IN CORSO...";
					$this->do_redirect($a_query_parameter->sql_values[0]);
					
					
				break;				
			}
		}
        // C
		public function create_tags_structure()
        {
            //echo __METHOD__."<BR>";
            global $g_javascript_namespace;
			parent::create_tags_structure();
			// method

			// component
            $local_name = $this->common_properties_array[t_base_field::common_properties_position_per_field_name];
            $local_current_tag = parent::add_tag(t_base_tag_type::btt_div,
                $local_name);
			$this->main_tag = $local_current_tag;
            $this->default_start_tag = $this->main_tag;
            $this->default_result_tag = $this->main_tag;
            //
			global $g_ambient_namespace;
			if ($g_ambient_namespace->user_agent->is_bot_agent)
				return ;
			switch ($this->proper_properties_array[self::proper_properties_position_redirect_type]) 
			{

				case self::redirect_type_from_address:
					
				break;
				case self::redirect_type_from_db:
					
				break;
				case self::redirect_type_from_custom_file:

				break;
				case self::redirect_type_from_js_redirect:

				break;		
				case self::redirect_type_from_js_db_redirect:

				break;					
			}	
            
            
        }
		public function create_javascript_initialization_component()
		{
			global $g_javascript_namespace;
			global $g_ambient_namespace;
			if ($g_ambient_namespace->user_agent->is_bot_agent)
				return ;
			switch ($this->proper_properties_array[self::proper_properties_position_redirect_type]) 
			{

				case self::redirect_type_from_address:
					
				break;
				case self::redirect_type_from_db:
					
				
				
					
				break;
				case self::redirect_type_from_custom_file:
					
				break;
				case self::redirect_type_from_js_redirect:

				break;		
				case self::redirect_type_from_js_db_redirect:
					
				
					
				break;
			
								
			}		
		}
		// D
		public function do_redirect($a_destination)
        {	
			global $g_javascript_namespace;
			global $g_ambient_namespace;
			if ($g_ambient_namespace->user_agent->is_bot_agent)
				return ;
			switch ($this->proper_properties_array[self::proper_properties_position_redirect_type]) 
			{

				case self::redirect_type_from_address:
					
				break;
				case self::redirect_type_from_db:
					header("Location: $a_destination");
				
				
					
				break;
				case self::redirect_type_from_custom_file:
					$local_file_name = trim(str_replace('.php','',$this->proper_properties_array[self::proper_properties_position_param_1]));
					$local_file_name = CUTEENGINE_PATH_CUSTOM_FILES.DIRECTORY_SEPARATOR.$local_file_name.".php";
					require($local_file_name);
					
				break;
				case self::redirect_type_from_js_redirect:

				break;		
				case self::redirect_type_from_js_db_redirect:
					
				
					$g_javascript_namespace->header_script_generator->add_method_to_a_class_with_content(
                get_class($this),'do_redirection','',"window.location.replace('".$a_destination ."');");
					
					$g_javascript_namespace->header_script_generator->add_content_to_a_method(get_class($this),
						t_header_script_generator::create_label,"
				 this.timerID = setTimeout (this.do_redirection.set_scope(this), ".$this->proper_properties_array[self::proper_properties_position_param_3].");
				");
				break;
			
								
			}	

		}	        
			
			
				
      
		// F
		public function finalize_creation_field()
        {
			
            global $g_cuteengine;
			global $g_ambient_namespace;
			if ($g_ambient_namespace->user_agent->is_bot_agent)
				return ;
			parent::finalize_creation_field();
			switch ($this->proper_properties_array[self::proper_properties_position_redirect_type]) 
			{

				case self::redirect_type_from_address:
						
				break;
				case self::redirect_type_from_db:
					//echo "finalize_creation_field<br>";
					$local_result = new t_content_loader_result;
					$local_action = $g_cuteengine->actions_list[md5($this->get_action_name('from_query'))];
					$local_action->do_action_from_ajax($local_result);
					/*if (! $local_result->succeeded)
					{
						throw new Exception("ERROR, azione initialization eseguita con errore!");
					}*/
				
				
					
				break;
				case self::redirect_type_from_custom_file:
					$this->do_redirect('');
				break;
				case self::redirect_type_from_js_redirect:

				break;		
				case self::redirect_type_from_js_db_redirect:
					//echo "finalize_creation_field<br>";
					$local_result = new t_content_loader_result;
					$local_action = $g_cuteengine->actions_list[md5($this->get_action_name('from_query'))];
					$local_action->do_action_from_ajax($local_result);
					/*if (! $local_result->succeeded)
					{
						throw new Exception("ERROR, azione initialization eseguita con errore!");
					}*/
					
				break;					
			}	

		}	        
		// G
		public function get_language_position_last()
        {
            return self::language_position_last;
        }
		public function get_result_action_script($a_event_type,$parent_event_type,
            $a_parameter,$a_content_loader_result)
		{
            return '';
        }
		// I
		public function initialize_field_event($a_event_type)
        {	
			
		}

    }
?>    